package Main;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;




public class leftBar extends Panel{
	public static Button textBtn, alphaBtn, bytBtn, histBtn, reset, draw, sixth; 
	public static boolean text=false;
	public static boolean alpha=false;
	public static boolean byt=false;
	public static Picture pic;
	public static BufferedImage dirty,clean;

	public leftBar() {
		setLayout(new GridLayout(7,1));
	    
		if(!text)
	    	textBtn = new Button("Encrypt Text");
	    else
	    	textBtn = new Button("Decrypt Text");
	    textBtn.addActionListener(new MyActionListener());
		
	   if(!alpha)
	    	alphaBtn = new Button("Encrypt Alpha");
	   else
	    	alphaBtn = new Button("Decrypt Alpha");
		alphaBtn.addActionListener(new MyActionListener());
		
	   if(!byt)
		  	bytBtn = new Button("Encrypt Byte");
	   else
		   bytBtn = new Button("Decrypt Byte");
		
	  	bytBtn.addActionListener(new MyActionListener());
		histBtn= new Button("Show Intensities");
		histBtn.addActionListener(new MyActionListener());
		draw=new Button("Draw Stuff!!");
		draw.addActionListener(new MyActionListener());
		
		reset=new Button("Reset");
		reset.addActionListener(new MyActionListener());
		
		sixth=new Button("Why is there a 7th button?");
		sixth.addActionListener(new MyActionListener());
		
		add(textBtn);
		add(alphaBtn);
		add(bytBtn);
		add(histBtn);
		add(draw);
		add(reset);
		add(sixth);
	}
	
	class MyActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) 
		{
			//String label = e.getActionCommand();
		    Object o = e.getSource();
		    
		    if(o==textBtn){
                
                if(!text){
                    GUI.openImageFile();
                    clean=GUI.getImageBuffered();
                    if(clean == null){
                        System.out.println("null img");
                    }
                    String inputValue = JOptionPane.showInputDialog("Please input a value");
                    dirty=StegMethods.encryptText(clean, inputValue);
                    
                    text=true;
                    //textBtn = new Button("Decrypt Text");
                    GUI.setImageColors(dirty);
                }
                else{
                	dirty = GUI.getImageBuffered();
                    JOptionPane.showMessageDialog(null, "Select the original picture to see the message");
                    GUI.openImageFile();
                    clean = GUI.getImageBuffered();
                    
                    
                    String string=StegMethods.decryptText(dirty,clean);
                    System.out.println(string);
                    JOptionPane.showMessageDialog(null, "The encrypted text was: "+ string);
                    GUI.setImageColors(clean);

                    //return clean;
                }
            }
		    
		    if(o==alphaBtn){
		    	if(!alpha){
		    		/**
		    		JOptionPane.showMessageDialog(null, "Pick your main image followed by the image you wish to hide.");
		    		GUI.openImageFile();
		    		clean=GUI.getImageBuffered();
		    		BufferedImage hidden = null;
		    		GUI.openImageFile();
		    		hidden=GUI.getImageBuffered();
		    		
		    		dirty=StegMethods.encryptAlpha(clean, hidden);
		    		GUI.setImageColors(dirty);
		    		alpha=true;
		    		//alphaBtn = new Button("Decrypt Alpha");

		    		//return dirty;
		    		 * 
		    		 */
		    		JOptionPane.showMessageDialog(null, "Pick your main image followed by the image you wish to hide.");
		    		GUI.openImageFile();
		    		clean=GUI.getImageBuffered();
		    		BufferedImage hidden = null;
		    		GUI.openImageFile();
		    		hidden=GUI.getImageBuffered();
		    		dirty=StegMethods.encryptByte(clean, hidden);
		    		alpha=true;
		    		GUI.setImageColors(dirty);
		    	}
		    	else{
		    		/**
		    		BufferedImage hidden2=StegMethods.decryptAlpha(dirty, clean);
		    		GUI.setImageColors(hidden2);
		    		**/
		    		BufferedImage hidden=StegMethods.decryptByte(dirty);
		    		GUI.setImageColors(hidden);

		    		//return hidden;
		    	}}
		    if(o==bytBtn){
		    	if(!byt){
		    		JOptionPane.showMessageDialog(null, "Pick your main image followed by the image you wish to hide.");
		    		GUI.openImageFile();
		    		clean=GUI.getImageBuffered();
		    		BufferedImage hidden = null;
		    		GUI.openImageFile();
		    		hidden=GUI.getImageBuffered();
		    		dirty=StegMethods.encryptByte(clean, hidden);
		    		byt=true;
		    		GUI.setImageColors(dirty);
		    		//bytBtn = new Button("Decrypt Byte");

		    		//return dirty;
		    	}
		    	else{
		    		BufferedImage hidden=StegMethods.decryptByte(dirty);
		    		GUI.setImageColors(hidden);

		    		//return hidden;
		    	}
		    }
		    if(o==histBtn){
		    	Color[][] c=GUI.getImageColors();
		    	System.out.println(c);
		    	Histogram h = new Histogram(Picture.averageRGB(c));
		        Picture pHist = new Picture(512,512,"Histogram");
		        pHist.setColors(h.blackLine(pHist.getColors()));
		        pHist.setColors(h.redLines(pHist.getColors()));
		        pHist.show();
		    }
		    if(o==draw){
		    	JOptionPane.showMessageDialog(null,"Nope!", "Haha, Deal With It!",JOptionPane.ERROR_MESSAGE);
		    }
		    if(o==reset){
		    	text=false;
		    	byt=false;
		    	alpha=false;
		    	//add something that sets it to null background.
		    }
		    if(o==sixth){
		    	final JFrame penguinFrame = new JFrame();
		    	penguinFrame.setSize(700, 700);
		    	final JTextArea penguinText = new JTextArea(100, 100);
		    	
		    	//JOptionPane.showMessageDialog(null,
		    	String penguin =
		    			"                 .88888888:.\n" +
		    			"                88888888.88888.\n" +
		    			"              .8888888888888888.\n" +
		    			"              888888888888888888\n" +
		    			"              88' _`88'_  `88888\n" +
		    			"              88 88 88 88  88888\n" +
		    			"              88_88_::_88_:88888\n" +
		    			"              88:::,::,:::::8888\n" +
		    			"              88`:::::::::'`8888\n" +
		    			"             .88  `::::'    8:88.\n" +
		    			"            8888            `8:888.\n" +
		    			"          .8888'             `888888.\n" +
		    			"         .8888:..  .::.  ...:'8888888:.\n" +
		    			"        .8888.'     :'     `'::`88:88888\n" +
		    			"       .8888        '         `.888:8888.\n" +
		    			"      888:8         .           888:88888\n" +
		    			"    .888:88        .:           888:88888:\n" +
		    			"    8888888.       ::           88:888888\n" +
		    			"    `.::.888.      ::          .88888888\n" +
		    			"   .::::::.888.    ::         :::`8888'.:.\n" +
		    			"  ::::::::::.888   '         .::::::::::::\n" +
		    			"  ::::::::::::.8    '      .:8::::::::::::.\n" +
		    			" .::::::::::::::.        .:888:::::::::::::\n" +
		    			" :::::::::::::::88:.__..:88888:::::::::::'\n" +
		    			"  `'.:::::::::::88888888888.88:::::::::'\n" +
		    			"        `':::_:' -- '' -'-' `':_::::'` +\n" +
		    			"		    			\n" +
		    			" _______ _______ _______     _______ _______ _______ _______ _______ \n" +
		    			"|   | |  | |  |_|  |  |  _    ||       ||    _ |  |       ||      | \n" +
		    			"|   | |__| |       |  | |_|   ||   _   ||   | ||  |    ___||  _    |\n" +
		    			"|   |      |       |  |       ||  | |  ||   |_||_ |   |___ | | |   |\n" +
		    			"|   |      |       |  |  _   | |  |_|  ||    __  ||    ___|| |_|   |\n" +
		    			"|   |      | ||_|| |  | |_|   ||       ||   |  | ||   |___ |       |\n" +
		    			"|___|      |_|   |_|  |_______||_______||___|  |_||_______||______|\n" +
		    			"\n"
		       		
		    			;
		    	penguinText.setText(penguin);
		    	penguinFrame.add(penguinText);
		    	penguinFrame.setVisible(true);
		    }
		   if(!text)
		    	textBtn.setLabel("Encrypt Text");
		   else
		    	textBtn.setLabel("Decrypt Text");
		   if(!alpha)
		    	alphaBtn.setLabel("Encrypt Alpha");
		   else
		    	alphaBtn.setLabel("Decrypt Alpha");
		   if(!byt)
			  	bytBtn.setLabel("Encrypt Byte");
		   else
			   bytBtn.setLabel("Decrypt Byte");
		    
		    
		    	}
		    }
	    
	}